<?php


/**
 * Skeleton subclass for representing a row from the 'seo_sites_paths' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package    lib.model
 */
class SeoSitePath extends BaseSeoSitePath {

	
	/**
	 * @param SeoUrl $url
	 * @return bool
	 */
	public function matches(SeoUrl $url)
	{
		if($this->getProtocol() && $this->getProtocol() != $url->getProtocol())
			return false;
		
		if($this->getPort() && $this->getPort() != $url->getPort())
			return false;
		
		if(!$this->domainMatches($url->getDomain()))
			return false;
		
		if(!$this->pathMatches($url->getPath()))
			return false;
			
		return true;
	}
	
	/**
	 * @param string $domain
	 * @return bool
	 */
	public function domainMatches($domain)
	{
		$domainPattern = $this->getDomain();
		if(!$domainPattern || $domainPattern == $domain)
			return true;
			
		$pattern = '/^' . str_replace(array('.', '*'), array('\.?', '.*'), $domainPattern) . '$/';
//		Ezer_Log::debug("Matching domain [$domain] to patern [$pattern]");
		return preg_match($pattern, $domain);
	}
	
	/**
	 * @param string $path
	 * @return bool
	 */
	public function pathMatches($path)
	{
		$pathPattern = $this->getPath();
		if(!$pathPattern || $pathPattern == $path)
			return true;
			
		$pattern = '/^' . str_replace(array('*', '/'), array('.*', '\/?'), $pathPattern) . '$/';
//		Ezer_Log::debug("Matching path [$path] to patern [$pattern]");
		return preg_match($pattern, $path);
	}
	
} // SeoSitePath
